$(function() {
	var innerID;
	var url = ctxPath + "/sys/user/getUser.do";
	var data = {};
	doGetReq(url, data, function(rsp) {
		var retData = rsp.retData;
		var payAccount = retData.payAccount;
		var name = retData.name;
		var qq = retData.qq;
		innerID = retData.innerID;
		if(payAccount != null && name != null && payAccount != "" && name != "") {
			$("#userTable tbody tr").html("<td>"+payAccount+"</td>" +
								"<td>"+name+"</td>" +
								"<td><input class='btn btn-default btn-xs' type='button' value='解绑' onclick='unbindPay(this);' data-innerID='"+innerID+"' /></td>");
			$("#userTable").show();
			$("#userForm").hide();
		} else{
			$("#userTable").hide();
			$("#userForm").show();
		}
		if(qq != null && qq != "") {
			$("#qqTable tbody tr").html("<td>"+qq+"</td>" +
								"<td><input class='btn btn-default btn-xs' type='button' value='更新' onclick='updateQQ(this);' data-innerID='"+innerID+"' data-qq='"+qq+"'/></td>");
			$("#qqTable").show();
			$("#qqForm").hide();
		} else{
			$("#qqTable").hide();
			$("#qqForm").show();
		}
		getInviteRegisterUrl(innerID);
		initRebateTable(innerID);
		initChildTable(innerID);
	});
	
	$('#myTabs a').click(function(e) {
		e.preventDefault();
		$(this).tab('show');
	});
	
	$("#childRebate-orderYear").datetimepicker({
		format: 'yyyy',
		language: "zh-CN",
		autoclose: true,
		startView: "4",
		minView: "4",
		maxView: "4"
	});
	
	$("#childRebate-orderMonth").datetimepicker({
		format: 'mm',
		language: "zh-CN",
		autoclose: true,
		startView: "3",
		minView: "3",
		maxView: "3"
	});
	
	$('#pwdForm').bootstrapValidator({
		live : 'disabled',
		feedbackIcons : {
			valid : 'glyphicon glyphicon-ok',
			invalid : 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields : {
			password : {
				validators : {
					notEmpty : {
						message : '旧密码不能为空.'
					}
				}
			},
			newPwd : {
				validators : {
					notEmpty : {
						message : '新密码不能为空.'
					}
				},
				stringLength : {
					min : 6,
					max : 32,
					message : '必须在6~32字符长度之间.'
				}
			},
			confirmPwd : {
				validators : {
					notEmpty : {
						message : '确认密码不能为空.'
					},
					stringLength : {
						min : 6,
						max : 32,
						message : '必须在6~32字符长度之间.'
					},
					same: {
                        field: 'newPwd',
                        message: '确认密码不一致.'
                    },
                    remote: {
						url: ctxPath + '/checkPwd.do',
						data: function(validator) {
							return {
								password: validator.getFieldElements('password').val(),
								innerID: innerID
							};
						},
						message: '旧密码错误.'
					}
				}
			}
		},
		submitHandler: function(validator, form, submitButton) {
			var url = ctxPath + "/changePwd.do";
			var newPwd = $("#newPwd").val();
			var data = {"innerID": innerID, "newPwd": newPwd};
			doPostReq(url, data, function(rsp) {
				alert("修改成功,请重新登录");
				var url = ctxPath + "/sys/user/logout.do";
				var data = {};
				doPostReq(url, data, function(rsp) {
					location.href = ctxPath + "/login.html";
				});
			});
		}
	});
	
	$('#pwdSaveBtn').click(function() {
		var password = $("#password").val();
		var newPwd = $("#newPwd").val();
		var confirmPwd = $("#confirmPwd").val();
		if($.trim(password) != "") {
			$("#password").val($.md5(password));
		}
		if($.trim(newPwd) != "") {
			$("#newPwd").val($.md5(newPwd));
		}
		if($.trim(confirmPwd) != "") {
			$("#confirmPwd").val($.md5(confirmPwd));
		}
		$("#pwdForm").submit();
    });
	
	$("#queryChildRebateData").click(function() {
		initChildTable(innerID);
	});
	
	$('#userForm').bootstrapValidator({
		live : 'disabled',
		feedbackIcons : {
			valid : 'glyphicon glyphicon-ok',
			invalid : 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields : {
			payAccount : {
				validators : {
					notEmpty : {
						message : '支付宝账户不能为空.'
					},
					stringLength : {
						min : 6,
						max : 32,
						message : '必须在6~32字符长度之间.'
					},
					remote: {
						url: ctxPath + '/sys/user/checkPayAccount.do',
						message: '该支付宝已被绑定.'
					}
				}
			},
			name : {
				validators : {
					notEmpty : {
						message : '姓名不能为空.'
					}
				},
				stringLength : {
					min : 6,
					max : 32,
					message : '必须在6~32字符长度之间.'
				}
			}
		},
		submitHandler: function(validator, form, submitButton) {
			var url = ctxPath + "/sys/user/updateUserPay.do";
			var payAccount = $("#payAccount").val();
			var name = $("#name").val();
			var data = {"innerID": innerID, "payAccount": payAccount, "name": name};
			doPostReq(url, data, function(rsp) {
				$("#userTable tbody tr").html("<td>"+payAccount+"</td>" +
											"<td>"+name+"</td>" +
											"<td>" +
												"<input class='btn btn-default btn-xs' type='button' value='解绑' onclick='unbindPay(this);' data-innerID='"+innerID+"'/>" +
											"</td>");
				$("#userTable").show();
				$("#userForm").hide();
				alert("绑定成功");
			});
		}
	});
	
	$('#qqForm').bootstrapValidator({
		live : 'disabled',
		feedbackIcons : {
			valid : 'glyphicon glyphicon-ok',
			invalid : 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields : {
			qq : {
				validators : {
					notEmpty : {
						message : 'QQ号不能为空.'
					}
				},
				stringLength : {
					min : 5,
					max : 20,
					message : '必须在5~20字符长度之间.'
				}
			}
		},
		submitHandler: function(validator, form, submitButton) {
			var url = ctxPath + "/sys/user/updateUserQQ.do";
			var qq = $("#qq").val();
			var data = {"innerID": innerID, "qq": qq};
			doPostReq(url, data, function(rsp) {
				$("#qqTable tbody tr").html("<td>"+qq+"</td>" +
											"<td>" +
												"<input class='btn btn-default btn-xs' type='button' value='更新' onclick='updateQQ(this);' data-innerID='"+innerID+"' data-qq='"+qq+"'/>" +
											"</td>");
				$("#qqTable").show();
				$("#qqForm").hide();
				$('#qqForm').data('bootstrapValidator').resetForm();
				alert("绑定成功");
			});
		}
	});
	
	var clip = new ZeroClipboard($("#copyYourUrl"));
	clip.on("ready", function() {
		this.on("aftercopy", function(event) {
			alert("复制成功");
		});
	});
	clip.on("error", function(event) {
    	ZeroClipboard.destroy();
  	});
});

function unbindPay(obj) {
	var url = ctxPath + "/sys/user/updateUserPay.do";
	var innerID = $(obj).attr("data-innerID");
	var data = {"innerID": innerID};
	doPostReq(url, data, function(rsp) {
		$('#userForm').data('bootstrapValidator').resetForm(true);
		$("#userTable").hide();
		$("#userForm").show();
		alert("解绑成功");
	});
}

function updateQQ(obj) {
	var qq = $(obj).attr("data-qq");
	$("#qq").val(qq);
	$("#qqTable").hide();
	$("#qqForm").show();
}

function initRebateTable(innerID) {
	return $("#orderTable").DataTable({
        "bProcessing": true,
        "bServerSide": true,
        "bDestroy" : true,
        "bFilter": false,
        "bLengthChange": false,
        "bAutoWidth" : true,
		"sAjaxDataProp" : "aaData",
		"bSort" : false,
		"sAjaxSource" : ctxPath + "/sys/order/queryRebate.do",
        "aoColumns": [
			{"mData": "orderDate"},
			{"mData": "rebate"},
			{"mData": function(order) {
				var count = order.count;
				var isRebate = order.isRebate;
				if(count >= isRebate) {
					return "未发放";
				} else if(count < isRebate) {
					return "已发放";
				}
			}}],
		"fnServerParams": function(aoData) {
			aoData.push({
				"name" : "innerID",
				"value" : innerID
			});
		},
		"fnServerData": function(sSource, aoData, fnCallback) {
			$.ajax({
				"dataType": 'json',
				"type": "POST",
				"url": sSource,
				"data": aoData,
				"success": function(resp) {
					var retCode = resp.retCode;
					if (retCode == "SUCCESS") {
						fnCallback(resp.retData);
					}
				}
			});
		}	
	});
}

function initChildTable(innerID) {
	return $("#childTable").DataTable({
        "bProcessing": true,
        "bServerSide": true,
        "bDestroy" : true,
        "bFilter": false,
        "bLengthChange": false,
        "bAutoWidth" : true,
		"sAjaxDataProp" : "aaData",
		"bSort" : false,
		"sAjaxSource" : ctxPath + "/sys/order/queryChildRebate.do",
        "aoColumns": [
            {"mData": "username"},
			{"mData": "orderDate"},
			{"mData": "parentRebate"},
			{"mData": function(order) {
				var count = order.count;
				var isParentRebate = order.isParentRebate;
				if(count >= isParentRebate) {
					return "未返利";
				} else if(count < isParentRebate) {
					return "已返利";
				}
			}}],
		"fnServerParams": function(aoData) {
			aoData.push({
				"name" : "innerID",
				"value" : innerID
			});
			var orderYear = $("#childRebate-orderYear").val();
			if (orderYear != undefined) {
				aoData.push({
					"name" : "orderYear",
					"value" : orderYear
				});
			}
			var orderMonth = $("#childRebate-orderMonth").val();
			if (orderMonth != undefined) {
				aoData.push({
					"name" : "orderMonth",
					"value" : orderMonth
				});
			}
		},
		"fnServerData": function(sSource, aoData, fnCallback) {
			$.ajax({
				"dataType": 'json',
				"type": "POST",
				"url": sSource,
				"data": aoData,
				"success": function(resp) {
					var retCode = resp.retCode;
					if (retCode == "SUCCESS") {
						fnCallback(resp.retData);
					}
				}
			});
		}	
	});
}

function getInviteRegisterUrl(innerID) {
	var url = ctxPath + "/sys/user/getInviteRegisterUrl.do";
	var data = {};
	doGetReq(url, data, function(rsp) {
		var retData = rsp.retData;
		$("#yourUrlInput").val(retData + "?innerID=" + innerID);
	})
}